package com.zqw.controller;

import com.zqw.info.AddressInfo;
import com.zqw.info.GoodsInfo;
import com.zqw.info.OrderInfo;
import com.zqw.info.UserInfo;
import com.zqw.pojo.*;
import com.zqw.service.OrderService;
import com.zqw.ss.AuthCheckAnno;
import com.zqw.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    //结算
    @RequestMapping("settlement")
    @ResponseBody
    @AuthCheckAnno
    public String settlement(String[] goods, String addid,HttpSession session){
        if(goods==null||goods.length<=0){
            return "0001";
        }
        List<GoodsInfo> shopCt = (List<GoodsInfo>)session.getAttribute("shopCt");
        UserInfo user = (UserInfo)session.getAttribute("user");
        String s = orderService.addOrder(shopCt, user.getCid(), goods, addid);
        if (s!=null){
            Date date = new Date();
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    OrderInfo orderInfo = new OrderInfo();
                    orderInfo.setOstatus(5);
                    orderInfo.setOid(s);
                    orderService.updOrders(orderInfo);
                    timer.cancel();
                }
            }, 30 * 60*1000);
            session.setAttribute(s,timer);
            return s;
        }else{
            return "-1";
        }
    }

    //跳转到地址界面
    @RequestMapping("checkout1")
    @AuthCheckAnno
    public String checkout1(HttpServletRequest request,String goods){
        if(goods==null||goods==""){
            return "error";
        }
        UserInfo user = (UserInfo)request.getSession().getAttribute("user");
        List<AddressPojo> addressPojos = orderService.queryAddress(String.valueOf(user.getCid()));
        request.setAttribute("goods",goods);
        request.setAttribute("add",addressPojos);
        return "checkout";
    }

    //添加地址
    @RequestMapping("newAdders")
    @AuthCheckAnno
    @ResponseBody
    public String newAdders(AddressInfo addOrder,HttpServletRequest request){
        UserInfo user = (UserInfo)request.getSession().getAttribute("user");
        addOrder.setCid(user.getCid());
        addOrder.setAddid(UUIDUtil.getUUID());
        boolean b = orderService.addAddress(addOrder);
        if(b){
            return addOrder.getAddid();
        }else{
            return "-1";
        }
    }

    //跳转到结算
    @RequestMapping("checkout3")
    @AuthCheckAnno
    public String checkout3(HttpServletRequest request,String goods){
        List<GoodsInfo> shopCt = (List<GoodsInfo>) request.getSession().getAttribute("shopCt");
        List<GoodsInfo> orders = new ArrayList<>();
        String[] split = goods.split(",");
        float sum = 0;
        float newSum = 0;
        for (int i = 0; i < shopCt.size(); i++) {
            for (int j = 0; j < split.length; j++) {
                if(shopCt.get(i).getGoodsId().equals(split[j])){
                    sum+=shopCt.get(i).getGoodsPrice()*shopCt.get(i).getGoodsNumber();
                    newSum+=shopCt.get(i).getGoodsPrice()*shopCt.get(i).getGoodsNumber()*shopCt.get(i).getGoodsDiscount();
                    orders.add(shopCt.get(i));
                }
            }
        }
        request.setAttribute("orders",orders);
        request.setAttribute("sum",sum);
        request.setAttribute("newSum",newSum);
        return "checkout4";
    }

    //跳转到添加地址
    @RequestMapping("checkout2")
    @AuthCheckAnno
    public String checkout2(HttpServletRequest request,String goods){
        List<GoodsInfo> shopCt = (List<GoodsInfo>) request.getSession().getAttribute("shopCt");
        String[] split = goods.split(",");
        float sum = 0;
        float newSum = 0;
        for (int i = 0; i < shopCt.size(); i++) {
            for (int j = 0; j < split.length; j++) {
                if(shopCt.get(i).getGoodsId().equals(split[j])){
                    sum+=shopCt.get(i).getGoodsPrice()*shopCt.get(i).getGoodsNumber();
                    newSum+=shopCt.get(i).getGoodsPrice()*shopCt.get(i).getGoodsNumber()*shopCt.get(i).getGoodsDiscount();
                }
            }
        }
        request.setAttribute("sum",sum);
        request.setAttribute("newSum",newSum);
        return "checkout1";
    }

    //跳转到付款
    @RequestMapping("checkout4")
    @AuthCheckAnno
    public String checkout4(HttpServletRequest request,String oid){
        OrderPojo orderPojo = orderService.queryOrder(oid);
        float sum = 0;
        float newSum = 0;
        if (orderPojo != null){
            List<OrderDetailsPojo> ods = orderPojo.getOds();
            for (int i = 0; i < ods.size(); i++) {
                sum+=ods.get(i).getGoods().getGoodsPrice()*ods.get(i).getGoods().getGoodsNumber();
                newSum+=ods.get(i).getGoods().getGoodsDiscount()*ods.get(i).getGoods().getGoodsPrice()*ods.get(i).getGoods().getGoodsNumber();
            }
        }
        request.setAttribute("sum",sum);
        request.setAttribute("newSum",newSum);
        return "checkout3";
    }

    //付款
    @RequestMapping("payment")
    @ResponseBody
    @AuthCheckAnno
    public String payment(HttpServletRequest request,String payment,String oid){
        boolean payment1 = orderService.payment(payment, oid);
        if(payment1){
            Timer attribute = (Timer)request.getSession().getAttribute(oid);
            attribute.cancel();
            request.getSession().removeAttribute(oid);
            return "0000";
        }else{
            return "0001";
        }
    }

    //跳到个人中心
    @RequestMapping("mini")
    @AuthCheckAnno
    public String mini(String ccid,String oid,HttpServletRequest request){
        if("2".equals(ccid)){
            request.setAttribute("ccid","2");
            return "mini";
        }else if("3".equals(ccid)){
            request.setAttribute("ccid","3");
            return "mini";
        }else if("4".equals(ccid)){
            request.setAttribute("ccid","4");
            request.setAttribute("oid",oid);
            return "mini";
        }else{
            request.setAttribute("ccid","1");
            return "mini";
        }
    }

    //跳到订单
    @RequestMapping("customer1")
    @AuthCheckAnno
    public String customer1(HttpServletRequest request){
        UserInfo user = (UserInfo)request.getSession().getAttribute("user");
        List<OrderPojo> orderPojos = orderService.queryOrders(String.valueOf(user.getCid()));
        request.setAttribute("orders",orderPojos);
        return "customer1";
    }

    //跳到地址
    @RequestMapping("customer2")
    @AuthCheckAnno
    public String customer2(HttpServletRequest request){
        UserInfo user = (UserInfo)request.getSession().getAttribute("user");
        List<AddressPojo> addressPojos = orderService.queryAddress(String.valueOf(user.getCid()));
        request.setAttribute("add",addressPojos);
        return "customer2";
    }

    //跳到订单详情
    @RequestMapping("customer4")
    @AuthCheckAnno
    public String customer4(HttpServletRequest request,String oid){
        OrderPojo orderPojo = orderService.queryOrder(oid);
        float sum = 0;
        float newSum = 0;
        if (orderPojo != null){
            List<OrderDetailsPojo> ods = orderPojo.getOds();
            for (int i = 0; i < ods.size(); i++) {
                sum+=ods.get(i).getGoods().getGoodsPrice()*ods.get(i).getOdnumber();
                newSum+=ods.get(i).getGoods().getGoodsDiscount()*ods.get(i).getGoods().getGoodsPrice()*ods.get(i).getOdnumber();
            }
        }
        request.setAttribute("sum",sum);
        request.setAttribute("newSum",newSum);
        request.setAttribute("order",orderPojo);
        return "customer4";
    }

    //查询所有的省
    @RequestMapping("queryProvince")
    @ResponseBody
    public List<ProvincePojo> queryProvince(){
        return orderService.queryProvince();
    }

    //查询所有的市
    @RequestMapping("queryCity")
    @ResponseBody
    public List<CityPojo> queryCity(String id){
        if (id==null){
            return null;
        }
        return orderService.queryCity(id);
    }

    //查询所有的省
    @RequestMapping("queryDistrict")
    @ResponseBody
    public List<DistrictPojo> queryDistrict(String id){
        if (id==null){
            return null;
        }
        return orderService.queryDistrict(id);
    }

    //删除地址
    @RequestMapping("delAddr")
    @ResponseBody
    public String delAddr(String addid){
        boolean b = orderService.delAddr(addid);
        if(b){
            return "0000";
        }else{
            return "0001";
        }
    }

}
